package com.flansmod.common;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CraftingInstance {
    //Input fields
    public IInventory inventory;
    public List<ItemStack> requiredStacks;
    public List<ItemStack> outputStacks;

    //Output fields
    public boolean craftingSuccessful;

    /**
     * The second IInventory is an empty one to copy into
     */
    public CraftingInstance(IInventory i, List<ItemStack> in, List<ItemStack> out) {
        inventory = i;
        requiredStacks = in;
        outputStacks = out;
    }

    public CraftingInstance(IInventory i, ArrayList<ItemStack> in, ItemStack out) {
        this(i, in, Collections.singletonList(out));
    }

    public boolean canCraft(EntityPlayer player) {
        craftingSuccessful = true;
        for (ItemStack check : requiredStacks) {
            int numMatchingStuff = 0;
            for (int j = 0; j < inventory.getSizeInventory(); j++) {
                ItemStack stack = inventory.getStackInSlot(j);
                if (stack != null && stack.getItem() == check.getItem() && stack.getItemDamage() == check.getItemDamage()) {
                    numMatchingStuff += stack.stackSize;
                }
            }
            if (numMatchingStuff < check.stackSize) {
                craftingSuccessful = false;
            }
        }
        return craftingSuccessful || player.capabilities.isCreativeMode;
    }

    public void craft(EntityPlayer player) {
        if (!craftingSuccessful)
            return;

        if (!player.capabilities.isCreativeMode) {
            for (ItemStack remove : requiredStacks) {
                int amountLeft = remove.stackSize;
                for (int j = 0; j < inventory.getSizeInventory(); j++) {
                    ItemStack stack = inventory.getStackInSlot(j);
                    if (amountLeft > 0 && stack != null && stack.getItem() == remove.getItem() && stack.getItemDamage() == remove.getItemDamage()) {
                        amountLeft -= inventory.decrStackSize(j, amountLeft).stackSize;
                    }
                }
            }
        }

        for (ItemStack stack : outputStacks)
            if (!player.inventory.addItemStackToInventory(stack))
                player.dropPlayerItemWithRandomChoice(stack, false);
    }
}
